package com.diqye.controller.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.diqye.common.CommonController;
import com.diqye.controller.user.tab.user_tab;
import com.diqye.controller.util.CKit;
import com.diqye.db.Tinfo;
import com.diqye.db.manage.XManage;
import com.diqye.db.model.XModel;
import com.diqye.templet.util.Strings;

public class User extends CommonController {

	public User() {
		super(user_tab.class);
	}
	
	public void save(){
		String qq=getParams().getT("qq");
		long l=db.findLong("select count(*) from user_tab where qq=#{0}",qq);
		if(l>0){
			renderOk("你所保存的QQ号已经存在");
			return;
		}
		getParams().s("birthday", "1991-12-04");
		getParams().s("createdate",new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
		super.save();
	}
	
	public void deleteTag(){
		XModel model = getParams();
		String loginqq=CKit.getLoginQQ(getRequest());
		boolean ok=true;
		Tinfo tinfo=XManage.getTinfo(tinfoClass);
		String key = tinfo.getPrimaryKey().get(0);
		List<String> keyValues = model.getT(key);
		String type = model.gts("type");
		if(type!=null){
			if("string".equals(type)){
				for(int i=0;i<keyValues.size();i++){
					if(keyValues.get(i).equals(loginqq))ok=false;
					keyValues.set(i, "'"+keyValues.get(i)+"'");
				}
			}
		}
		if(ok==false){
			renderErr("不能删除自己");
			return;
		}
		
		int result = db.update("update  ${0} set isdelete='1' where ${1} in(${2})",
				tinfo.getTablename(),
				key,
				Strings.join(keyValues, ","));
		renderOk(result);
	}
	
	public void modify(){
		if("".equals(getParams().gts("password"))){
			getParams().s("password", null);
		}
		super.modify();
	}
	
	public void query(){
		String sql="select qq,sname,name,phone,createdate,sex,city from user_tab ",
				sql2="select count(*) from user_tab ",
				where="where isdelete='0' ",
				limit=" limit #{2},#{3} ",
				orderby=" order by createdate desc";
		if(!"".equals(getParams().gts("qq"))){
			where+=" and qq=#{0} ";
		}
		if(!"".equals(getParams().gts("sname"))){
			 where+="and sname like '%${1}%' ";
		}
		if(!"".equals(getParams().gts("sex"))){
			where+="and sex = #{4} ";
		}
		if(getParams().gts("city")!=null&&!"".equals(getParams().gts("city"))){
			where+="and city like '%${5}%' ";
		}
		@SuppressWarnings("rawtypes")
		List r = db.find(sql+where+orderby+limit
				, getParams().gts("qq")
				,getParams().gts("sname")
				,getParams().getInt("currentIndex")
				,getParams().getInt("pageSize")
				,getParams().gts("sex")
				,getParams().gts("city"));
		long len=db.findLong(sql2+where, getParams().gts("qq"),getParams().gts("sname"),null,null,getParams().gts("sex"),getParams().gts("city"));
		renderOk(new XModel().s("count",len).s("list",r));
	}
	
	public void queryOne(){
		String qq=getParams().gts("qq"),
				sql="select name,sname,qq,sex,phone,city,province,createdate,note,birthday from user_tab where qq =#{0}";
		renderOk(db.findFirst(sql, qq));
		
	}
	
	public void imsg(){
		Object login=CKit.getLogin(getRequest());
		if(login==null){
			XModel nologin=CKit.getNoLogin(getRequest());
			if(nologin==null){
				List list;
				String sql="select t1.funid as \"code\",t1.funname as \"name\" from fun_tab t1 inner join role_fun_tab t2 on t1.funid=t2.fun";
				list=db.find(sql+" and t2.roleid='guest'");
				getRequest().getSession().setAttribute("nologin", new XModel().s("login", false).s("menulist",list));
				imsg();
			}else{
				renderOk(nologin);
			}
			
		}else{
			renderOk(login);
		}
	}
	
	public void login(){
		String username=getParams().gts("user_account"),password=getParams().gts("user_password");
		XModel user=db.findFirst("select qq,name,sname from user_tab where qq=#{0} and password=#{1} and isdelete='0' ", username,password),login;
		if(user==null){
			renderErr("用户名或密码不正确");
		}else{
			String sql="select t1.funid as \"code\",t1.funname as \"name\" from fun_tab t1 inner join role_fun_tab t2 on t1.funid=t2.fun",
			sql2="SELECT roleid FROM role_user_tab WHERE userid=#{0}";
			List menulist;
			String role = db.findString(sql2, user.gts("qq"));
			if(role==null){
				role="v1";
				menulist=db.find(sql+" and t2.roleid='v1'");
			}else{
				menulist=db.find(sql+" and t2.roleid=#{0}",role);
			}
			login=new XModel();
			login.s("login", true)
			.s("qq", user.gts("qq"))
			.s("name",user.gts("name"))
			.s("sname",user.gts("sname"))
			.s("role",role)
			.s("menulist", menulist);
			getRequest().getSession().setAttribute("login", login);
			renderOk(null);
		}
	}
	
	public void loginout(){
		getRequest().getSession().removeAttribute("login");
		renderOk(null);
	}
	
	public void msglist(){
		renderOk(CKit.getMsgList());
	}
	
	public void sendmsg(){
		String datestr=CKit.getCurrentDate(),author;
		if(CKit.isLogin(getRequest())){
			author=CKit.getLoginSname(getRequest());
		}else{
			author=getRequest().getRemoteAddr();
		}
		
		CKit.addmsg(new XModel().s("date", datestr).s("msg",getParams().gts("msg")).s("author", author));
		msglist();
	}

	
}
